package com.kkd.kkdapp.activity;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.utils.KkdPlayerManager;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import com.kkd.kkdapp.utils.KkdPlayerManager.PlayerCallback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;

public class PlayerActivity extends Activity {

	private SurfaceView mSurfaceView;
	private KkdPlayerManager mPlayer;
	private SeekBar mSeekBar;
	private Button btnPause, btnPlayUrl, btnStop;
	String msURI;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_ask);
		mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
		mSeekBar = (SeekBar) findViewById(R.id.skbProgress);
		btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
		btnPlayUrl.setOnClickListener(new ClickEvent());

		btnPause = (Button) this.findViewById(R.id.btnPause);
		btnPause.setOnClickListener(new ClickEvent());

		btnStop = (Button) this.findViewById(R.id.btnStop);
		btnStop.setOnClickListener(new ClickEvent());
		mSeekBar.setOnSeekBarChangeListener(new SeekBarChangeEvent());
		mPlayer = new KkdPlayerManager(mSurfaceView, mSeekBar,new PlayerCallback() {
			
			@Override
			public void onPrepared(MediaPlayer arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onCompletion(MediaPlayer arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public boolean onInfo(MediaPlayer mp, int what, int extra) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void onSeekComplete(MediaPlayer mp) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void onBufferingUpdate(MediaPlayer arg0,
					int bufferingProgress) {
				// TODO Auto-generated method stub
				
			}
		});
		mSurfaceView.getHolder().addCallback(new Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				if(mPlayer!=null&&mPlayer.mediaPlayer.isPlaying()==false)
					mPlayer.playUrl(msURI);
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {
				// TODO Auto-generated method stub
				
			}
		});
//		 msURI="http://live.kuaikuaidai.com/AppName/StreamName.flv";
		 msURI="http://live.kuaikuaidai.com/AppName/StreamName.m3u8";
		 
		 
		 
//		 msURI="rtmp://live.kuaikuaidai.com/AppName/StreamName";
		// msURI="http://video.kuaikuaidai.com/video/爱剪辑-我的视频3秒.mp4";
//		 msURI="http://video.kuaikuaidai.com/video/爱剪辑-给你看-长视频.mp4";
//		 msURI="http://video.kuaikuaidai.com/video/John Lennon - Imagine HD.mp4";
//		msURI = "http://video.kuaikuaidai.com/video/John%20Lennon%20-%20Imagine%20HD.mp4";
//		mPlayer.playUrl(msURI);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		if(mPlayer!=null)
			mPlayer.pause();
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		if(mPlayer!=null){
			mPlayer.stop();
			mPlayer = null;
		}
		super.onDestroy();
	}

	class ClickEvent implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			if (arg0 == btnPause) {
				mPlayer.pause();
			} else if (arg0 == btnPlayUrl) {
				mPlayer.playUrl(msURI);
			} else if (arg0 == btnStop) {
				mPlayer.stop();
			}
		}
	}

	class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
		int progress;

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()
			this.progress = progress * mPlayer.mediaPlayer.getDuration()
					/ seekBar.getMax();
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {

		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// seekTo()的参数是相对与影片时间的数字，而不是与seekBar.getMax()相对的数字
			mPlayer.mediaPlayer.seekTo(progress);
		}
	}
}